#!/usr/bin/env python
# -*- coding: utf-8 -*-
# (c) Anton A Nesterov
# License: Apache2.0

from .config import app

################################################################################

from model.index import Post
from wtforms.ext.appengine.db import model_form
from bottle import request, response, abort, redirect

@app.get('/')
def home(wrap):
  wrap.set(cart={}) #set some data in the session, for example

  post = Post.get('index')
  print post
  if post:
    print post.files
  return wrap.render('index.haml', post = post)


@app.get('/editor/')
@app.get('/editor/<id>')
def get_editor(wrap, id=None):

  if id:
    post = Post.get_by_id(long(id))
    form = model_form(Post)(title = post.title, text = post.text)
  else:
    form = model_form(Post)()

  records = Post.all().run(limit=10)
  return wrap.render('editor.haml', form=form, action=id, records=records)

@app.post('/editor/')
def post_editor(wrap):

  Post.put(**request.POST)

  return redirect('/editor/')


@app.post('/editor/<id>')
def put_editor(wrap, id):

  Post.put(**request.POST)

  return redirect('/editor/%s'%id)

@app.delete('/editor/<id>')
def del_editor(wrap, id):

  Post.rem(long(id))

  return {}

################################################################################

@app.get('/files/')
def file_manage(wrap):
  return wrap.render('uploads.haml')

